﻿/***************************************************
//  Copyright (c) Premium Tax Free 2011
/***************************************************/

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using PremierTaxFree.PTFLib.Extensions;

namespace PremierTaxFree.Tools
{
    public class PenTool : BaseTool
    {
        private Point m_StartPoint, m_CurrentPoint;
        private bool m_Started = false;

        public PenTool(CanvasControl canvas)
            : base(canvas)
        {
        }

        protected override void MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                m_StartPoint = m_CurrentPoint = Control.MousePosition;
                m_Started = true;
                Cursor.Clip = m_Canvas.RectangleToScreen(m_Canvas.ClientRectangle);
            }
            else
            {
                m_Started = false;
            }
        }

        protected override void MouseMove(object sender, MouseEventArgs e)
        {
            if (m_Started)
            {
                Debug.WriteLine(m_Canvas.LineSize);
                ControlPaint.DrawReversibleLine(m_StartPoint, m_CurrentPoint, Color.Black);
                m_CurrentPoint = Control.MousePosition;
                ControlPaint.DrawReversibleLine(m_StartPoint, m_CurrentPoint, Color.Black);
                FireInfo("Length", m_StartPoint.LengthTo(m_CurrentPoint).ToString("F"));
            }
        }

        protected override void MouseUp(object sender, MouseEventArgs e)
        {
            if (m_Started)
            {
                SaveTool();
                Cursor.Clip = Rectangle.Empty;
                m_Started = false;
            }
        }

        protected override void MouseLeave(object sender, EventArgs e)
        {
            if (m_Started)
            {
                SaveTool();
                Cursor.Clip = Rectangle.Empty;
                m_Started = false;
            }
        }

        protected override void SaveTool()
        {
            Bitmap desktop = ImageHelper.GetDesktopImage();
            Bitmap bmp = desktop.Crop(m_Canvas.RectangleToScreen(m_Canvas.ClientRectangle));

            Image img = m_Canvas.BackgroundImage;
            ImageHelper.UpdateSafe(ref img, bmp);
            m_Canvas.BackgroundImage = img;
        }
    }
}
